//
//  MSDetailContactsCell.m
//  SocietySale
//
//  Created by Mike on 16/11/10.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSDetailContactsCell.h"

@interface MSDetailContactsCell ()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *companyLabel;
@property (nonatomic, strong) UILabel *positionLabel;
@property (nonatomic, strong) UILabel *telephoneLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *sourceLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView  *bottomView;
@property (nonatomic, strong) GXButton *lookBtn;
@property (nonatomic, strong) GXButton *saleContactsBtn;
@end

@implementation MSDetailContactsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        self.contentView.clipsToBounds = YES;
    }
    return self;
}

- (void)initView {
    UIButton *guideBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    guideBtn.frame = CGRectMake(SCREEN_WIDTH-82, 10, 70, 30);
    guideBtn.backgroundColor = kBlueColor;
    guideBtn.titleLabel.font = HeitiFont(14);
    [guideBtn setTitle:@"找人脉" forState:UIControlStateNormal];
    [guideBtn addTarget:self action:@selector(guideClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:guideBtn];
    
    self.nameLabel = [self creatLabel:CGRectMake(12, 10, 260, 35)];
    self.nameLabel.font = HeitiFont(15);
    
    self.companyLabel = [self creatLabel:CGRectMake(12, self.nameLabel.bottom, SCREEN_WIDTH-90, 30)];
    self.positionLabel = [self creatLabel:CGRectMake(12, self.companyLabel.bottom, SCREEN_WIDTH-24, 30)];
    self.telephoneLabel = [self creatLabel:CGRectMake(12, self.positionLabel.bottom, SCREEN_WIDTH-24, 30)];
    self.addressLabel = [self creatLabel:CGRectMake(12, self.telephoneLabel.bottom, SCREEN_WIDTH-24, 30)];
    
    _bottomView =[[UIView alloc] initWithFrame:CGRectMake(0, self.addressLabel.bottom+10, SCREEN_WIDTH, 100)];
    [self.contentView addSubview:_bottomView];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH-24, 0.5)];
    horizonLineView.backgroundColor = kNormalGray;
    [_bottomView addSubview:horizonLineView];
    
    UIView *horizonLineView2 =[[UIView alloc] initWithFrame:CGRectMake(0, 50.5, SCREEN_WIDTH, 0.5)];
    horizonLineView2.backgroundColor = kNormalGray;
    [_bottomView addSubview:horizonLineView2];
    
    //有3位会员正在出售该信息
    _lookBtn = [self creatBtnWithFrame:CGRectMake(12, 0.5, SCREEN_WIDTH-24, 50) andTitle:@"查看他的其他项目"];
    [_lookBtn addTarget:self action:@selector(lookClick) forControlEvents:UIControlEventTouchUpInside];

    
    
    
    self.sourceLabel = [self creatBottomLabel:CGRectMake(12, 65, SCREEN_WIDTH/2, 20)];
    self.timeLabel = [self creatBottomLabel:CGRectMake(SCREEN_WIDTH-92, 65, 80, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    
    UIButton *correctBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    correctBtn.frame = CGRectMake(SCREEN_WIDTH/2-20, 63, 78, 24);
    correctBtn.titleLabel.font = HeitiFont(14);
    correctBtn.layer.cornerRadius = 2;
    correctBtn.layer.borderColor = kNormalGray.CGColor;
    correctBtn.layer.borderWidth = 1;
    [correctBtn setTitle:@"纠错有奖" forState:UIControlStateNormal];
    [correctBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [correctBtn addTarget:self action:@selector(correctClick) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:correctBtn];
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, _bottomView.bottom, SCREEN_WIDTH, 8)];
    _bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_bgView];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
//    label.textColor = kDarkGrayColor;
    [self.contentView addSubview:label];
    return label;
}

//创建label
- (UILabel*)creatBottomLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kLightBlackColor;
    [_bottomView addSubview:label];
    return label;
}

//创建btn
- (GXButton*)creatBtnWithFrame:(CGRect)frame andTitle:(NSString*)title{
    GXButton *btn = [GXButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.relayout = YES;
    btn.titleLabel.font = HeitiFont(14);
    btn.titleLabelFrame = CGRectMake(0, 0, SCREEN_WIDTH-40, 50);
    btn.imageViewFrame =  CGRectMake(SCREEN_WIDTH-44, 16, 9, 17);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"arrow2"] forState:UIControlStateNormal];
    [btn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
    [_bottomView addSubview:btn];
    return btn;
}

- (void)setModel:(MSDetailContactModel *)model{
    _model = model;
    self.nameLabel.text = [NSString stringWithFormat:@"%@   (%@)",model.name,model.contactType];
    self.companyLabel.attributedText = [self getMutableAttriStrWithForStr:@"公司    " backStreet:model.company];
    self.positionLabel.attributedText = [self getMutableAttriStrWithForStr:@"职位    " backStreet:model.position];
    self.telephoneLabel.attributedText = [self getMutableAttriStrWithForStr:@"电话    " backStreet:model.telephone];
    self.addressLabel.attributedText = [self getMutableAttriStrWithForStr:@"地址    " backStreet:model.address];
    self.sourceLabel.attributedText =  [self getMutableAttriStrWithForStr:@"数据来源：" backStreet:model.source];
    
    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time];
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    
    self.timeLabel.attributedText = timeAttstr;
    
    if (model.isSaling) {
        [_lookBtn setTitle:[NSString stringWithFormat:@"有%@位会员正在出售该信息",model.otherProject] forState:UIControlStateNormal];
    }
    else{
        [_lookBtn setTitle:@"查看他的其他项目" forState:UIControlStateNormal];
    }
}

- (NSMutableAttributedString*)getMutableAttriStrWithForStr:(NSString*)forStr backStreet:(NSString*)backStr{
    NSMutableAttributedString *AttStr = [[NSMutableAttributedString alloc] initWithString:forStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSAttributedString *backAttStr = [[NSAttributedString alloc] initWithString:backStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kLightBlackColor}];
    [AttStr appendAttributedString:backAttStr];
    
    return AttStr;
}

- (void)guideClick{
    if ([self.delegate respondsToSelector:@selector(ContactSClick:andModel:)]) {
        [self.delegate ContactSClick:ContactSClickType_Guide andModel:_model];
    }
}

- (void)correctClick{
    if ([self.delegate respondsToSelector:@selector(ContactSClick:andModel:)]) {
        [self.delegate ContactSClick:ContactSClickType_Correct andModel:_model];
    }
}

- (void)lookClick{
    if (_model.isSaling) {
        if ([self.delegate respondsToSelector:@selector(ContactSClick:andModel:)]) {
            [self.delegate ContactSClick:ContactSClickType_Saling andModel:_model];
        }
    }
    else{
        if ([self.delegate respondsToSelector:@selector(ContactSClick:andModel:)]) {
            [self.delegate ContactSClick:ContactSClickType_Look andModel:_model];
        }
    }
}

+ (CGFloat)cellHeightWithModel:(MSDetailContactModel*)model{
    return 280;
}

@end
